//
//  HMSubJsonViewController.m
//  HMPersonalDoctor
//
//  Created by GD on 16/9/10.
//  Copyright © 2016年 geduo. All rights reserved.
//

#import "HMSubJsonViewController.h"
#import <AFNetworking.h>
#import "HMDoctorIntroCell.h"
#import "HMHomeSubViewController.h"

static NSString *doctorCellID = @"doctorCellID";

@interface HMSubJsonViewController ()<UITableViewDelegate,UITableViewDataSource,HMDoctorIntroCellDelegate>

@property (nonatomic,weak) UITableView *tableView;

@end

@implementation HMSubJsonViewController{
    NSArray *_doctorIntroList;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = self.subTitle;
    
    [self setupUI];
}

-(void)setupUI{
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    tableView.backgroundColor = [UIColor lightGrayColor];
    
//    tableView.contentInset = UIEdgeInsetsMake(10, 10, 0, 10);//为什么上边可以有效果,而右侧没有效果?  与首页collectionView 类比???
    tableView.delegate = self;
    
    tableView.dataSource = self;
    
    [tableView registerNib:[UINib nibWithNibName:@"HMDoctorIntroCell" bundle:nil] forCellReuseIdentifier:doctorCellID];
    
    self.tableView = tableView;
    
    
    [self.view addSubview:tableView];


}


#pragma  mark  UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _doctorIntroList.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HMDoctorIntroCell *cell = [tableView dequeueReusableCellWithIdentifier:doctorCellID forIndexPath:indexPath];
    
    cell.model = _doctorIntroList[indexPath.row];
    
    cell.delegate = self;
    
    return cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150;
}

//-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
//    return 10;
//}
//-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
//    return  [UIButton buttonWithType:UIButtonTypeContactAdd];
//}

#pragma mark - HMDoctorIntroCellDelegate
-(void)doctorIntroCell:(HMDoctorIntroCell *)doctorIntroCell didClickZiXun:(NSString *)url{
    HMHomeSubViewController *subVc = [[HMHomeSubViewController alloc] init];
    
    subVc.url = url;
    
    [self.navigationController pushViewController:subVc animated:YES];
}

-(void)doctorIntroCell:(HMDoctorIntroCell *)doctorIntroCell didClickZhuanZhen:(NSString *)url{
    HMHomeSubViewController *subVc = [[HMHomeSubViewController alloc] init];
    
    subVc.url = url;
    
    [self.navigationController pushViewController:subVc animated:YES];
    
}


-(void)setUrl:(NSString *)url{
    _url = url;
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
     manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        NSLog(@"responseObject===%@",responseObject);
        NSString *str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        
        NSLog(@"str====%@",str);//非json解析
//       NSString *str1 = [str substringFromIndex:@"([{".length];
//        NSLog(@"str1=====%@",str1);
        NSRange startFlag = [str rangeOfString:@"([{"];
        NSRange endFlag = [str rangeOfString:@"}])"];
        NSRange subRange = NSMakeRange(startFlag.location + 1, endFlag.location - startFlag.location + 1);
        NSString *subStr = [str substringWithRange:subRange];
        NSLog(@"开始%@",subStr);
        NSData;
        
        NSArray *array = [NSJSONSerialization JSONObjectWithData: [subStr dataUsingEncoding:NSUTF8StringEncoding] options:0 error:NULL];
        NSMutableArray *tempArray = [NSMutableArray array];
       [array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
           HMDoctorIntroModel *model = [[HMDoctorIntroModel alloc] init];
           [model setValuesForKeysWithDictionary:obj];
           
           [tempArray addObject:model];
       }];
        _doctorIntroList = tempArray.copy;
        
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
   
}




@end
